Skip to content

Improve error message for mandatory attributes after defaults - #1639

Open
Topgearfanboy wants to merge 1 commit into
python-attrs:mainfrom
Topgearfanboy:main
Open

Topgearfanboy wants to merge 1 commit into
python-attrs:mainfrom
Topgearfanboy:main

Conversation

@Topgearfanboy

Copy link
Copy Markdown

Summary

When a mandatory attribute appears after an attribute with a default value or factory, the error message now includes which attribute has the default value. This makes it much easier to identify the problem and fix the attribute ordering.

Previously, the error message only mentioned the mandatory attribute that lacked a default:

No mandatory attributes allowed after an attribute with a default value or factory.  Attribute in question: Attribute(name='y', ...)

Now it also shows which attribute has the default:

No mandatory attributes allowed after an attribute with a default value or factory.  Attribute in question: Attribute(name='y', ...) appears after Attribute(name='x', default=None, ...)

This addresses the issue raised in #437 where users struggled to understand which attributes were out of order, especially in complex inheritance scenarios or when using field transformers.

Test plan

  • Updated existing tests in tests/test_make.py and tests/test_hooks.py to match the new error message format
  • All existing tests pass with the new message format
  • The change is purely a message improvement with no behavior changes

When a mandatory attribute appears after an attribute with a default
value or factory, the error message now includes which attribute has
the default, making it easier to identify the problem.

Fixes python-attrs#437

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant